<?php
include_once("{$_SERVER['DOCUMENT_ROOT']}/Common/Data/MySQLDAO.php");
include("MediaBean.php");

class MediaDAO extends MySQLDAO {
	protected function lookupSqlById($id) {
		switch($id) {
			case "SELECT":
				return "select title,url,posted_date,description,speaker,subtitle,summary,duration,keywords,id,coll_id from media where id = ?";
				break;
			case "INSERT":
				return "insert into media (title,url,posted_date,description,speaker,subtitle,summary,duration,keywords,id,coll_id) values (?,?,?,?,?,?,?,?,?,?,?)";
				break;
			case "UPDATE":
				return "update media set title = ?,url = ?,posted_date = ?,speaker = ?,coll_id = ? where id = ?";
				break;
			case "DELETE":
				return "delete from media where id = ?";
				break;
			default:
				return;
				break;
		}
	}

	protected function bindParameters($id,$paramBean) {
		switch($id) {
			case "SELECT":
				$params[0] = $this->bindInt($paramBean->getId());
				break;
			case "INSERT":
				$params[0] = $this->bindString($paramBean->getTitle());
				$params[1] = $this->bindString($paramBean->getUrl());
				$params[2] = $this->bindDate($paramBean->getPostedDate());
				$params[3] = $this->bindString($paramBean->getDescription());
				$params[4] = $this->bindString($paramBean->getSpeaker());
				$params[5] = $this->bindString($paramBean->getSubtitle());
				$params[6] = $this->bindString($paramBean->getSummary());
				$params[7] = $this->bindString($paramBean->getDuration());
				$params[8] = $this->bindString($paramBean->getKeywords());
				$params[9] = $this->bindInt($paramBean->getId());
				$params[10] = $this->bindInt($paramBean->getCollId());
				break;
			case "UPDATE":
				$params[0] = $this->bindString($paramBean->getTitle());
				$params[1] = $this->bindString($paramBean->getUrl());
				$params[2] = $this->bindDate($paramBean->getPostedDate());
				$params[3] = $this->bindString($paramBean->getSpeaker());
				$params[4] = $this->bindInt($paramBean->getCollId());
				$params[5] = $this->bindInt($paramBean->getId());
				break;
			case "DELETE":
				$params[0] = $this->bindInt($paramBean->getId());
				break;
		}
		return $params;
	}

	protected function loadBean($id,$row,$rows) {
		$bean = new MediaBean();
		switch($id) {
			case "SELECT":
				$bean->setTitle($row['title']);
				$bean->setUrl($row['url']);
				$bean->setPostedDate($row['posted_date']);
				$bean->setDescription($row['description']);
				$bean->setSpeaker($row['speaker']);
				$bean->setSubtitle($row['subtitle']);
				$bean->setSummary($row['summary']);
				$bean->setDuration($row['duration']);
				$bean->setKeywords($row['keywords']);
				$bean->setId($row['id']);
				$bean->setCollId($row['coll_id']);
				break;
		}
		array_push($rows,$bean);
		return $rows;
	}

	protected function getDbPropertiesFile($file="") {
    	try {
			parent::getDbPropertiesFile("Media.properties");
        }
		catch(Exception $e) {
			print('<br><br><span style="color:red;">There was an error loading the properties file!!</span>');
	        exit();
        }
	}
}
?>
